//
//  Status.swift
//  DSWeibo
//
//  Created by liuzhu on 2018/11/23.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class Status: NSObject{

    var created_at:String?
    
    var id:Int = 0
    
    var text:String?
    
    var source:String?
    
    var pic_urls:[[String:AnyObject]]?
    
    var user:User?
    //加载微博数据
    class func loadStatuses(finsihed:@escaping (_ models:[Status],_ error:NSError?)->()){
        let path = "2/statuses/home_timeline.json"
        let params = ["access_token":UserAccount.loadAccount()?.access_token]
        NetWorkTools.shareNetworkTools().get(path, parameters: params, progress: nil, success: { (_, JSON) in
            let dic = JSON as! [String:AnyObject]
            let models = dict2Model(list: dic["statuses"] as! [[String:AnyObject]])
            finsihed(models,nil)
        }) { (_, error) in
//           finsihed(nil,error)
        }
    }
    
    //将字段数组转换为模型数组
    class func dict2Model(list:[[String:AnyObject]])->[Status]{
        var models = [Status]()
        for dict in list{
            models.append(Status(dict: dict))
        }
        return models
    }
    
    //字典转模型
    init(dict:[String:AnyObject]){
        super.init()
        //setValuesForKeys(dict)
        created_at = dict["created_at"] as! String
        id = Int(dict["id"] as! NSNumber)
        text = dict["text"] as! String
        source = dict["source"] as! String
        pic_urls = dict["pic_urls"] as! [[String:AnyObject]]
        user = User(dict: dict["user"] as! [String:AnyObject])
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    
    //打印当前的模型
//    var properties = ["id","text","created_ad","source","pic_urls"]
//    override var debugDescription: String{
//        let dict = dictionaryWithValues(forKeys: properties)
//        return "\(dict)"
//    }
}
